package watermark;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import watermark.PictureCommonButton.ButtonStyle;

/**
 * 添加水印图片功能的按钮的监听器
 * @author syt
 */
public class PictureButtonAction implements ActionListener {
    private ButtonStyle style;
    private MainPictureFrame mainPictureFrame;
    private JFileChooser fileChooser = new JFileChooser();

    public PictureButtonAction(ButtonStyle style){
        this.style = style;
    }

    public void actionPerformed(ActionEvent e) {
        fileChooser.updateUI();// 将 UI 属性重置为当前的外观值。
        mainPictureFrame = MainPictureFrame.getInstance();
        //选择图片
        if (style.equals(ButtonStyle.selectImage)) {
            String result = getSelectResult();
            if (result != null) {
                mainPictureFrame.getFilepathTF().setText(result);
                mainPictureFrame.getSavepathTF().setText(
                        result.substring(0, result.lastIndexOf("\\")) + Common.getNewFileorDirName(result));
            }
        }
        //选择水印
        else if (style.equals(ButtonStyle.selectIcon)){
            String result = getSelectResult();
            if (result !=null){
                mainPictureFrame.getIconpathTF().setText(result);
            }
        }
        //选择保存路径
        else if (style.equals(ButtonStyle.selectSavepath)) {
            String result = getSelectResult();
            if (result != null) {
                mainPictureFrame.getSavepathTF().setText(
                        result + Common.getNewFileorDirName(mainPictureFrame.getFilepath()));
            }
        }
        //选择预览效果
        else if (style.equals(PictureCommonButton.ButtonStyle.preview)) {
            String firstFilepath = getFirstFilepath();
            if(firstFilepath ==  null){
                JOptionPane.showMessageDialog(null, "找不到图片文件！");
                return;
            }
            BufferedImage buffImg = null;
            try {
                buffImg = addWatermark(firstFilepath);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            new PreviewImage(buffImg);
        }
        //选择导出图片
        else if (style.equals(PictureCommonButton.ButtonStyle.drirect)) {
            String savePath = mainPictureFrame.getSavePath();
            if (savePath.equals("")) {
                JOptionPane.showMessageDialog(null, "请选择存放路径！");
                return;
            }
            // 对图像加水印
            BufferedImage buffImg = null;
            try {
                buffImg = addWatermark(mainPictureFrame.getFilepath());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            // 生成水印图片
            generate(buffImg, savePath);
            mainPictureFrame.getProgressBar().setValue(mainPictureFrame.getProgressBar().getMaximum());
            JOptionPane.showMessageDialog(null, "操作成功！");
        }else{
            JOptionPane.showMessageDialog(null, "还没添加此功能呀！亲！");
        }
    }

    /**
     *
     * @return 选中的文件的名字
     */
    private String getSelectResult(){
        if (style.equals(ButtonStyle.selectImage) || style.equals(ButtonStyle.selectIcon)) {
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "图片文件(*.bmp, *.gif, *.jpg, *.jpeg, *.png)", "bmp",
                    "gif", "jpg", "jpeg", "png");
            fileChooser.setFileFilter(filter);
            fileChooser.setDialogTitle("选择图片文件");
        }else{
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.setDialogTitle("选择文件");
        }
        int result = fileChooser.showDialog(null, "确定");
        if (result == JFileChooser.APPROVE_OPTION) {
            return fileChooser.getSelectedFile().getPath();
        }
        return null;
    }
    /**
     *
     * @return 预览时的图片的绝对路径
     */
    private String getFirstFilepath() {
        return mainPictureFrame.getFilepath();
    }
    /**
     * @param filepath 图片的绝对路径
     * @return 图像加水印之后的BufferedImage对象
     */
    private BufferedImage addWatermark(String filepath) throws IOException {
        String iconPath = mainPictureFrame.getIconpath();
        int toward = mainPictureFrame.getToward();
        float scale = mainPictureFrame.getScale();
        BufferedImage buffImg = PictureTool.watermark(iconPath, filepath, toward, scale);
        return buffImg;
    }
    /**
     * 生成水印图片
     * @param buffImg 图像加水印之后的BufferedImage对象
     * @param savePath 图像加水印之后的保存路径
     */
    private void generate(BufferedImage buffImg, String savePath) {
        int temp = savePath.lastIndexOf(".") + 1;
        try {
            ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

